home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / FINGSERV.C < prev    next >
C/C++ Source or Header  |  1990-05-31  |  5KB  |  215 lines

  1. /*
  2.  *
  3.  *    Finger support...
  4.  *
  5.  *    Finger server routines.     Written by Michael T. Horne - KA7AXD.
  6.  *    Copyright 1988 by Michael T. Horne, All Rights Reserved.
  7.  *    Permission granted for non-commercial use and copying, provided
  8.  *    that this notice is retained.
  9.  *    Modified for more memory-reasonable performance by PE1CHL.
  10.  *
  11.  */
  12.  
  13. #include <stdio.h>
  14. #ifdef MSC
  15. #include <sys/types.h>
  16. #endif
  17. #include "global.h"
  18. #include "mbuf.h"
  19. #include "timer.h"
  20. #include "internet.h"
  21. #include "icmp.h"
  22. #include "netuser.h"
  23. #include "tcp.h"
  24. #include "finger.h"
  25. #include "session.h"
  26.  
  27. struct tcb *fing_tcb = NULLTCB;
  28. int16 finger_notify = 0;
  29.  
  30. int
  31. fing1(argc, argv)
  32. int    argc;
  33. char    *argv[];
  34. {
  35.     extern int32    ip_addr;
  36.     struct socket    lsocket;
  37.     void        fing_state();
  38.     void        rcv_fing();
  39.     char        tos = 0;
  40.  
  41.     if (fing_tcb != NULLTCB)
  42.         return 1;
  43.     /* start finger daemon */
  44.     lsocket.address = ip_addr;
  45.     if(argc < 2)
  46.         lsocket.port = FINGER_PORT;
  47.     else {
  48.         if ((lsocket.port = atoi(argv[1])) == 0)
  49.             lsocket.port = FINGER_PORT;
  50.         tos = get_tos(argv[2]);
  51.     }
  52.     fing_tcb = open_tcp(&lsocket, NULLSOCK, TCP_SERVER, 0, rcv_fing,
  53.         NULLVFP, fing_state, tos, (int *)NULL);
  54.     finger_notify = (argc > 2);
  55.     return 0;
  56. }
  57. /*
  58.  *    Handle incoming finger connections and closures.
  59.  *
  60.  */
  61. void
  62. fing_state(tcb,old,new)
  63. struct tcb    *tcb;
  64. char        old,        /* old state */
  65.         new;        /* new state */
  66. {
  67.     struct finger    *fing;
  68.  
  69.     switch(new){
  70.     case ESTABLISHED:
  71.         log_tcp(tcb,"open Finger");
  72.         if ((fing = (struct finger *) calloc(1,sizeof(struct finger))) == NULLFING){
  73.             close_tcp(tcb);
  74.             return;
  75.         }
  76.  
  77.         tcb->user = (char *)fing;    /* Upward pointer */
  78.         fing->tcb = tcb;        /* Downward pointer */
  79.         if (finger_notify)  {
  80.             printf("\007You're being fingered by %s!\n",
  81.                 phsocket(&tcb->conn.remote));
  82.             fflush(stdout);
  83.         }
  84.         return;
  85.     case CLOSED:
  86.         if (tcb == fing_tcb)
  87.             fing_tcb = NULLTCB;
  88.         if ((struct finger *)tcb->user != NULLFING)
  89.             free(tcb->user);
  90.         del_tcp(tcb);
  91.         break;
  92.     }
  93. }
  94.  
  95. /*
  96.  *    Stop the finger server.
  97.  */
  98.  
  99. int
  100. fing0()
  101. {
  102.     if (fing_tcb != NULLTCB) {
  103.         close_tcp(fing_tcb);
  104.         fing_tcb = NULLTCB;
  105.     }
  106.     return 0;
  107. }
  108.  
  109. /*
  110.  *    Finger receive upcall.    This is the guts of the finger server.
  111.  *    The user to finger is read from the socket.  If only a newline
  112.  *    is read, then send the remote host a list of all known 'users' on
  113.  *    this system.
  114.  */
  115.  
  116. void
  117. rcv_fing(tcb, cnt)
  118. register struct tcb    *tcb;
  119. int16            cnt;
  120. {
  121.     struct finger    *fing;
  122.     struct mbuf    *bp;
  123.     char        *fingerpath,*getenv(),
  124.             who[FINGNAMELEN + 1],
  125.             path[80],
  126.             temp[80],
  127.             user[80];
  128.     int        size;
  129.     void xmt_fing();
  130.  
  131.     if ((fing = (struct finger *) tcb->user) == NULLFING ||      /* uh oh! */
  132.         (fingerpath = getenv(FINGERPATH)) == NULLCHAR ||
  133.         recv_tcp(tcb,&bp,FINGNAMELEN) == 0) {
  134.         close_tcp(tcb);
  135.         return;
  136.     }
  137.  
  138.     size = pullup(&bp, who, FINGNAMELEN);    /* get 'user' name */
  139.     who[size] = '\0';            /* NUL terminate it */
  140.     free_p(bp);                /* all done with bp */
  141.  
  142.     if (*who == '\r' || *who == '\n') {    /* give him a user listing */
  143.         int found = 0;
  144.  
  145.         /* create wildcard path to finger files */
  146.         strcpy(path, fingerpath);
  147.         strcat(path, "*");
  148.         strcat(path, fingersuf);
  149.  
  150.         send_tcp(tcb,qstring("Known users on this system:\r\n"));
  151.         for (filedir(path, 0, user); user[0] != '\0';
  152.             filedir (path, 1, user))  {
  153.             found++;
  154.             *index(user, '.') = '\0';
  155.             sprintf(temp, "        %s\r\n", user);
  156.             send_tcp(tcb,qstring(temp));
  157.         }
  158.         if (!found)
  159.             send_tcp(tcb,qstring("None!\r\n"));
  160.  
  161.         close_tcp(tcb);
  162.         return;
  163.     }
  164.  
  165.     /*
  166.      *    Create path to user's finger file and see if the
  167.      *    the file exists.
  168.      */
  169.     rip(who);
  170.     strcpy(path, fingerpath);
  171.     strcat(path, who);
  172.     strcat(path, fingersuf);
  173.  
  174.     if ((fing->fp = fopen(path, "r")) == NULL) {
  175.         sprintf(temp, "User %s unknown on this system\r\n",
  176.                   who);
  177.         send_tcp(tcb, qstring(temp));
  178.         close_tcp(tcb);            /* close socket */
  179.         return;
  180.     }
  181.  
  182.     /* File opened, send first block and leave rest to upcalls */
  183.  
  184.     tcb->r_upcall = NULLVFP;
  185.     (*(tcb->t_upcall = xmt_fing))(tcb,tcb->cwind);
  186. }
  187.  
  188. /* Finger transmit upcall.  Send next window-full of file data */
  189.  
  190. void
  191. xmt_fing(tcb, cnt)
  192. register struct tcb    *tcb;
  193. int16            cnt;
  194. {
  195.     struct finger *fing;
  196.     int sent = 0;
  197.     char temp[256];
  198.  
  199.     if ((fing = (struct finger *) tcb->user) == NULLFING)    /* uh oh! */
  200.         return;
  201.  
  202.     if (fing->fp != NULLFILE) {
  203.         while (fgets(temp,sizeof(temp),fing->fp) != NULLCHAR) {
  204.             rip(temp);
  205.             strcat(temp,"\r\n");
  206.             send_tcp(tcb,qstring(temp));
  207.             if ((sent += strlen(temp)) >= cnt)
  208.                 return;        /* window full, pause */
  209.         }
  210.         fclose(fing->fp);
  211.         fing->fp = NULLFILE;
  212.     }
  213.     close_tcp(tcb);
  214. }
  215.